package com.zhonghui.wms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhonghui.response.BaseResult;
import com.zhonghui.wms.domain.WmsWarehouse;
import com.zhonghui.wms.domain.WmsWarehouseSeat;
import com.zhonghui.wms.service.IWmsWarehouseSeatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.wms.domain.WmsWarehouseArea;
import com.zhonghui.wms.service.IWmsWarehouseAreaService;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.common.core.page.TableDataInfo;

/**
 * 库区Controller
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Api(tags="智能仓储WMS-库区")
@RestController
@RequestMapping("/wms/area")
public class WmsWarehouseAreaController extends BaseController
{
    @Autowired
    private IWmsWarehouseAreaService wmsWarehouseAreaService;

    @Autowired
    private IWmsWarehouseSeatService wmsWarehouseSeatService;


    /**
     * 查询库区列表
     */
    @ApiOperation("查询智能仓储WMS-库区列表")
    @GetMapping("/list")
    public TableDataInfo<List<WmsWarehouseArea>> list(WmsWarehouseArea wmsWarehouseArea)
    {
        startPage();
        List<WmsWarehouseArea> list = wmsWarehouseAreaService.selectWmsWarehouseAreaList(wmsWarehouseArea);
        return getDataTable(list);
    }
    /**
     * 查询库区列表不分页
     */
    @ApiOperation("查询智能仓储WMS-库区不分页")
    @GetMapping("/areaList")
    public BaseResult<List<WmsWarehouseArea>> areaList(WmsWarehouseArea wmsWarehouseArea)
    {
        return BaseResult.success(wmsWarehouseAreaService.selectWmsWarehouseAreaList(wmsWarehouseArea));
    }
    /**
     * 导出库区列表
     */
    @ApiOperation("导出智能仓储WMS-库区列表")
    @Log(title = "库区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsWarehouseArea wmsWarehouseArea)
    {
        List<WmsWarehouseArea> list = wmsWarehouseAreaService.selectWmsWarehouseAreaList(wmsWarehouseArea);
        ExcelUtil<WmsWarehouseArea> util = new ExcelUtil<WmsWarehouseArea>(WmsWarehouseArea.class);
        util.exportExcel(response, list, "库区数据");
    }

    /**
     * 获取库区详细信息
     */
    @ApiOperation("获取智能仓储WMS-库区详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<WmsWarehouseArea> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(wmsWarehouseAreaService.selectWmsWarehouseAreaById(id));
    }

    /**
     * 新增库区
     */
    @ApiOperation("新增智能仓储WMS-库区")
    @Log(title = "库区", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody WmsWarehouseArea wmsWarehouseArea)
    {
        return BaseResult.success(wmsWarehouseAreaService.insertWmsWarehouseArea(wmsWarehouseArea));
    }

    /**
     * 修改库区
     */
    @ApiOperation("修改智能仓储WMS-库区")
    @Log(title = "库区", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody WmsWarehouseArea wmsWarehouseArea)
    {
        return BaseResult.success(wmsWarehouseAreaService.updateWmsWarehouseArea(wmsWarehouseArea));
    }

    /**
     * 删除库区
     */
    @ApiOperation("删除智能仓储WMS-库区")
    @Log(title = "库区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        for (Long id : ids){
            WmsWarehouseSeat seat = new WmsWarehouseSeat();
            seat.setWarehouseAreaId(id);
            List<WmsWarehouseSeat> seatList = wmsWarehouseSeatService.selectWmsWarehouseSeatList(seat);
            if (seatList.size() > 0){
                return BaseResult.error("当前库区中存在库位，删除失败！！！");
            }else {
                wmsWarehouseAreaService.deleteWmsWarehouseAreaById(id);
            }
        }
        return BaseResult.success();
    }
}
